<?php

namespace Admin\Controller;

use Think\Model;

/**
 * Description of SpBookproductController
 *
 * @author Myron
 */
class SpBookproductController extends AdminController {

    /**
     * 展示门店采购列表
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function index() {
        $name = I('name');
        $shopId=I('shopId');
        $bookStatus=I('bookStatus');
        
        if($shopId>-1){
           $map['shopId'] =$shopId;
           $this->assign('shopId', $shopId);
        };
        
        if($bookStatus>-1){
           $map['bookStatus'] =$bookStatus;
           $this->assign('bookStatus', $bookStatus); 
        };
        $map['batchNum'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('sp_bookproduct', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $shoplist = M('sp_shops')->where(array('status' => 1))->select();
        $this->assign('shoplist', $shoplist);
        $this->meta_title = '门店订单清单';
        $this->display();
    }

    /**
     * 展示采购详情
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function detail() {
        $id = I('id');
        $BookproductData = M('sp_bookproduct')->where(array('id' => $id))->find();
        $map['bookprodId'] = $id;
        $list = M('table')->table('gr_sp_bookproddetail a,gr_sp_product b')->where('a.productId = b.id')->field('a.*,b.name as productname')->select();
        int_to_string($list);
        $this->assign('_list', $list);
        $this->assign('data', $BookproductData);
        switch($BookproductData['bookStatus']){
            case 0:
                $btnStr= "审核操作";
                break; 
            case 10:
                $btnStr= "开始配货";
                break;             
            case 30:
                $btnStr= "确认发货";
                break;   
        }
        $this->assign('btnStr', $btnStr); 
        $this->display();
        
        
    }

    /**
     * 状态修改
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function changeStatus($status = '', $id='',$sendNum='',$pid='',$remark='') { 
        $gr_sp_bookproduct = array(
            'bookStatus' => $status,
            'remark'=>$remark,
        ); 
        if($status==30){
            for($i=0;$i<count($pid);$i++){
                M('sp_bookproddetail')->where(array('id' => $pid[$i]))->save(array('sendNum'=>$sendNum[$i])); 
            }
        }        
        if (!M('sp_bookproduct')->where(array('id' => $id))->save($gr_sp_bookproduct)) {
            $this->error('操作失败！');
        } else {
            $this->success('操作成功！');
        };
    }

}
